﻿using System.Text;
using System.IO;

namespace 温湿度和颗粒度监控测试.Common
{
    public class FileHelper
    {
        /// <summary>
        /// 将内容写入文件中
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="content"></param>
        /// <param name="fileMode"></param>
        /// <param name="fileShare"></param>
        public static void Write(string filePath, string content, FileMode fileMode = FileMode.Create, FileShare fileShare = FileShare.Read)
        {
            using (FileStream fs = new FileInfo(filePath).Open(fileMode, FileAccess.Write, fileShare))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(content);
                fs.Write(buffer, 0, buffer.Length);
            }
        }

        /// <summary>
        /// 读取文件的内容，并返回字符串
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileMode"></param>
        /// <param name="fileShare"></param>
        /// <returns></returns>
        public static string Read(string filePath, FileMode fileMode = FileMode.Open, FileShare fileShare = FileShare.Read)
        {
            using (FileStream fs = new FileInfo(filePath).Open(fileMode, FileAccess.Read, fileShare))
            {
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);

                return Encoding.UTF8.GetString(buffer);
            }
        }
    }
}
